package com.qf.shop.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.shop.entity.*;
import com.qf.shop.service.AddressService;
import com.qf.shop.service.CartService;
import com.qf.shop.service.OrdersService;
import com.qf.shop.service.impl.AddressServiceImpl;
import com.qf.shop.service.impl.CartServiceImpl;
import com.qf.shop.service.impl.OrdersServiceImpl;
import com.qf.shop.utils.Constants;
import com.qf.shop.utils.RandomUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet("/order")
public class OrdersController extends BaseController {
    private OrdersService ordersService = new OrdersServiceImpl();

    //添加收货地址
    public String add(HttpServletRequest request, HttpServletResponse response) {
        //1.获取添加的订单的的用户id
        String uid = request.getParameter("uid");

        //2.调用业务逻辑
        //查询所有的收货地址
        AddressService addressService = new AddressServiceImpl();
        List<Address> addList = addressService.getAllAddressByUid(Integer.valueOf(uid));

        //查询所有的购物车商品
        CartService cartService = new CartServiceImpl();
        List<Cart> cartList = cartService.getCartById(Integer.valueOf(uid));
        //将收货地址、购物车所有商品、存在作用域
        request.setAttribute("addList", addList);
        request.setAttribute("cartList", cartList);
        return Constants.FORWARD + "/order.jsp";

    }

    //生成订单
    public String create(HttpServletRequest request, HttpServletResponse response) {
        //1.收参
        String uid = request.getParameter("uid");
        String sum = request.getParameter("sum");
        String aid = request.getParameter("aid");
        //2.调用业务逻辑生成订单
        Orders orders = new Orders(RandomUtils.createOrderId(), Integer.valueOf(uid), Integer.valueOf(sum), 1, new Date(), Integer.valueOf(aid));
        ordersService.createOrders(orders);

        //3.跳转订单展示页面
        return Constants.FORWARD + "/order?method=show";
    }

    //展示所有订单
    public String show(HttpServletRequest request, HttpServletResponse response) {

        //1.当前登录用户的id
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            request.setAttribute("msg", "查看订单请先登录！");
            return Constants.FORWARD + "/login.jsp";
        }
        //先获取当前登录用户的id
        int id = loginUser.getId();
        //2.调用业务查询当前用户的所有订单
        List<Orders> ordersList = ordersService.selectAllOrdersByUid(id);
        //3.存储作用域转发

        request.setAttribute("orderList", ordersList);
        return Constants.FORWARD + "orderList.jsp";


    }

    //展示订单详情
    public String showDetail(HttpServletRequest request, HttpServletResponse response) {
        //1.收参
        String oid = request.getParameter("oid");

        Orders od = ordersService.selectOrdersByOid(oid);

        request.setAttribute("od", od);

        return Constants.FORWARD + "/orderDetail.jsp";
    }

    //成功支付
    public String success(HttpServletRequest request, HttpServletResponse response) {
        //收参
        String oid = request.getParameter("oid");
        //获取的是支付结果的JSON数据
        String result = request.getParameter("result");

        try {
            //2.解析JSON数据封装成WeiXin
            ObjectMapper objectMapper = new ObjectMapper();
            WeiXin weiXin = objectMapper.readValue(result, WeiXin.class);
            String result_code = weiXin.getResult().getResult_code();
            //支付结果判断
            if (result_code != null && result_code.equals("SUCCESS")) {
                //支付成功
                //修改订单状态
                int i = ordersService.updateOrdersStatus(oid);
                //跳转查询所有订单
                return Constants.FORWARD + "/order?method=show";
            } else {
                //支付失败
                request.setAttribute("msg", "支付失败！");
                //提示
                //跳转message.jsp页面进行提示即可
                return Constants.FORWARD + "/message.jsp";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
